﻿using IdentityModel.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net.Http;

namespace IdentityServer4.Client.Client {
    class Program {

        static string ApirUrl;
        static string ServerUrl;

        static void Main(string[] args) {
            //启动保护的资源API项目：IdentityServer4.ApiResource
            ApirUrl = "http://localhost:5001/weatherforecast";

            //鉴权中心根据不同的模式启用不同的项目，端口都为5000
            ServerUrl = "http://localhost:5000";

            //客户端授权模式 IdentityServer4.ClientCredentials.Server
            //TestClientCredentials();

            //资源所有者密码授权 IdentityServer4.ResourceOwnerPassword.Server
            TestResourceOwnerPassword();

            Console.Read();
        }

        //客户端授权模式
        static async void TestClientCredentials() {
            //鉴权中心获取token
            var client = new HttpClient();
            var disco = await client.GetDiscoveryDocumentAsync("http://localhost:5000");
            if (disco.IsError) {
                Console.WriteLine(disco.Error);
                return;
            }
            var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest {
                Address = disco.TokenEndpoint,

                ClientId = "client",
                ClientSecret = "secret",
                Scope = "scope1"
            });

            if (tokenResponse.IsError) {
                Console.WriteLine(tokenResponse.Error);
                return;
            }

            Console.WriteLine(tokenResponse.Json);

            //使用token请求api
            var client2 = new HttpClient();
            client2.SetBearerToken(tokenResponse.AccessToken);

            var response2 = await client2.GetAsync(ApirUrl);
            if (!response2.IsSuccessStatusCode) {
                Console.WriteLine(response2.StatusCode);
            }
            else {
                var content = await response2.Content.ReadAsStringAsync();
                Console.WriteLine(JArray.Parse(content));
            }

        }

        //资源所有者密码授权
        static async void TestResourceOwnerPassword() {
            //鉴权中心获取token
            var client = new HttpClient();
            var disco = await client.GetDiscoveryDocumentAsync("http://localhost:5000");
            if (disco.IsError) {
                Console.WriteLine(disco.Error);
                return;
            }
            var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest {
                Address = disco.TokenEndpoint,

                ClientId = "client",
                ClientSecret = "secret",
                Scope = "scope1",
                
                UserName = "username1",
                Password = "password1",
            });

            if (tokenResponse.IsError) {
                Console.WriteLine(tokenResponse.Error);
                return;
            }

            Console.WriteLine(tokenResponse.Json);

            //使用token请求api
            var client2 = new HttpClient();
            client2.SetBearerToken(tokenResponse.AccessToken);

            var response2 = await client2.GetAsync(ApirUrl);
            if (!response2.IsSuccessStatusCode) {
                Console.WriteLine(response2.StatusCode);
            }
            else {
                var content = await response2.Content.ReadAsStringAsync();
                Console.WriteLine(JArray.Parse(content));
            }

        }
    }
}
